/*
    This file is part of Raptor.

    Raptor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Raptor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Raptor.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "include/raptor.h"
#include "include/importWizard.h"
#include "include/configWizard.h"

#include <QtSingleApplication>
#include <QtGui>
#include <QApplication>
#include <QNetworkProxy>
#include <QSettings>
#include <QTranslator>

int main(int argc, char *argv[])
{
    QtSingleApplication instance("raptor-loader", argc, argv);
    QString message;

    for(int a=1; a<argc; ++a)
    {
    	message += argv[a];
    	if(a < argc-1) message += " ";
    }

    if(instance.sendMessage(message)) return 0;

    //QCA::Initializer init;
    Q_INIT_RESOURCE(importWizard);
    Q_INIT_RESOURCE(configDialog);

    QCoreApplication::setOrganizationName("raptor-loader");
    QCoreApplication::setOrganizationDomain("raptor-loader.org");
    QCoreApplication::setApplicationName("Raptor");
    QCoreApplication::setApplicationVersion("0.16");

    QSettings settings("raptor-loader", "Raptor");

    bool configured = settings.value("configured", false).toBool();
    if(!configured)
    {
        configWizard cfgWizard;
        if(cfgWizard.exec())
        {
            settings.setValue("configured", true);
        }
    }

    QPointer<QSplashScreen> splash;
    bool useSplash = settings.value("showSplash", true).toBool();
    if(useSplash)
    {
        splash = new QSplashScreen(QPixmap(":/splash.png"));

        splash->show();
        qApp->processEvents();
    }

    //Sprache waehlen
    QString locale = settings.value("gui/language", QLocale::system().name()).toString();
    QTranslator translator;
    qDebug() << "translator: " << (QString("translations/raptor_") + locale) << translator.load(QString("translations/raptor_") + locale);
    instance.installTranslator(&translator);

    Raptor::S_PROXY proxy;

    proxy.host = settings.value("proxy/host", "").toString();
    proxy.port = settings.value("proxy/port", 0).toUInt();
    proxy.type = (QNetworkProxy::ProxyType) settings.value("proxy/type", QNetworkProxy::NoProxy).toInt();
    proxy.user = settings.value("proxy/user", "").toString();
    proxy.pass = settings.value("proxy/pass", "").toString();

    if(proxy.user.trimmed() == "")
        proxy.networkProxy = QNetworkProxy(proxy.type, proxy.host, proxy.port);
    else
        proxy.networkProxy = QNetworkProxy(proxy.type, proxy.host, proxy.port, proxy.user, proxy.pass);

    bool useSSL = settings.value("useSSL", true).toBool();

    QString folderRaptor = settings.value("folderRaptor", QDir::currentPath()).toString();
    QString folderDownload = settings.value("folderDownload", QDir::currentPath() + QDir::separator() + "downloads").toString();

    Raptor w(proxy, useSSL, folderRaptor, folderDownload, splash);
    w.handleMessage(message);

    w.restoreGeometry(settings.value("gui/geometry").toByteArray()); // fenstergroesse und position wiederherstellen
    w.restoreState(settings.value("gui/state").toByteArray(), 1);

    w.maxDownloads = settings.value("maxDownloads").toInt();

    w.setDownloadCollumsWidth(settings.value("gui/downloads").toString());
    w.setHistoryCollumsWidth(settings.value("gui/history").toString());
    w.show();

    QObject::connect(&instance, SIGNAL(messageReceived(const QString&)), &w, SLOT(handleMessage(const QString&)));

    instance.setActivationWindow(&w, false);
    QObject::connect(&w, SIGNAL(needToShow()), &instance, SLOT(activateWindow()));

    return instance.exec();
}
